package com.asset.inventory.view

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.KeyEvent
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.asset.inventory.BR
import com.asset.inventory.R
import com.asset.inventory.adapter.InventoryAdapter
import com.asset.inventory.base.BaseActivity
import com.asset.inventory.base.BaseApp
import com.asset.inventory.databinding.ActivityAssetInventoryRecordBinding
import com.asset.inventory.kt.hideSoftKeyboard
import com.asset.inventory.kt.startActivity
import com.asset.inventory.kt.toast
import com.asset.inventory.manager.RfidManager
import com.asset.inventory.model.TaskBean
import com.asset.inventory.viewModel.AssetInventoryRecordViewModel

/**
 *    @author : sibb
 *    date   : 21-3-16
 *    desc   : 资产盘点记录页面
 */
class AssetInventoryRecordActivity: BaseActivity<AssetInventoryRecordViewModel,ActivityAssetInventoryRecordBinding>() {

    private var mList = ArrayList<TaskBean.Task>()

    private  lateinit var exit: TextView

    private var exitTime = 0L

    private val taskAdapter: InventoryAdapter by lazy {
        InventoryAdapter(mList, object : InventoryAdapter.OnItemClickListener {

            override fun enterInventoryDetailPage(position: Int) {
                startActivity<AssetInventoryDetailActivity>(this@AssetInventoryRecordActivity) {
                    val bundle = Bundle()
                    bundle.putSerializable("TASK_DATA",mList[position])
                    putExtras(bundle)
                }

            }

            override fun commit(position: Int) {
                mList[position].id?.let {
                    mViewModel.commit(it)

                }
            }

            override fun clear(position: Int) {
            }
        })
    }

    override fun getLayoutId(): Int = R.layout.activity_asset_inventory_record

    override fun initVariableId(): Int = BR.assetInventoryRecordViewModel

    override fun initViewModel(): Class<AssetInventoryRecordViewModel> = AssetInventoryRecordViewModel::class.java

    override fun initView() {
        exit = binding.bar.findViewById<TextView>(R.id.right)
        binding.inventoryList.layoutManager = LinearLayoutManager(this)
        binding.inventoryList.adapter = taskAdapter
        RfidManager.instance.openSerial()

    }

    override fun initListener() {
        super.initListener()
        exit.setOnClickListener {
            BaseApp.instance.finishAllActivity()
            startActivity(Intent(this,LoginActivity::class.java))
        }

        mViewModel.observerTaskList().observe(this, Observer {
            if(mList.size > 0){
                mList.clear()
            }
            mList.addAll(it)
            taskAdapter.notifyDataSetChanged()

        })

        mViewModel.loadingLiveData().observe(this, Observer {
            if(it){
                showLoading()
            }else{
                closeLoading()
            }
        })
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp()
        }
        return true
    }

    private fun exitApp() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            toast("再按一次退出应用")
            exitTime = System.currentTimeMillis()
        } else {
            BaseApp.instance.finishAllActivity()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        RfidManager.instance.close()
    }

    fun closeLoading(){
        binding.loading1.visibility = View.GONE
        binding.avii.smoothToHide()
    }


    fun showLoading(){
        binding.loading1.visibility = View.VISIBLE
        binding.avii.smoothToShow()
    }



}